package com.example.configuration;

import lombok.SneakyThrows;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Xiancheng {

    public static void main(String[] args) throws InterruptedException {
        /*//第一种方式
        Thread myThread1 = new MyThread();
        myThread1.setName("1号窗口");
        myThread1.start();
        Thread myThread2 = new MyThread();
        myThread2.setName("2号窗口");
        myThread2.start();*/
        /*//第二种方式
        MyRunnable myRunnable = new MyRunnable();
        Thread thread1 = new Thread(myRunnable, "1号窗口");
        Thread thread2 = new Thread(myRunnable, "2号窗口");
        thread1.start();
        thread2.start();*/
        /*MyRunnable1 myRunnable1 = new MyRunnable1();
        Thread thread1 = new Thread(myRunnable1, "单数");
        Thread thread2 = new Thread(myRunnable1, "双数");
        thread1.start();
        thread2.start();*/
        //打印100为随机数
        MyRunnable2 MyRunnable2 = new MyRunnable2();
        Thread thread1 = new Thread(MyRunnable2, "随机数");
        thread1.start();
        Thread.sleep(1000*5);
        thread1.interrupt();

        //跳出循环
       /* MyRunnable3 MyRunnable3 = new MyRunnable3();
        Thread thread1 = new Thread(MyRunnable3, "跳出循环");
        thread1.start();
        Thread.sleep(10000);
        MyRunnable3.flag = true;*/
       //计算文件大小个数
       /* MyRunnable4 MyRunnable4 = new MyRunnable4();
        Thread thread1 = new Thread(MyRunnable4, "第一个");
        Thread thread2 = new Thread(MyRunnable4, "第二个");
        thread1.start();
        Thread.sleep(1000);
        thread2.start();*/
        /*//线程让步
        //当前线程没第一次抢到cpu时做出让步
        Thread.yield();
        //线程阻塞
        Thread myThread1 = new MyThread();
        myThread1.start();
        //阻塞当前线程直至执行完毕在运行其他线程
        myThread1.join();
        //线程睡眠   执行到这的时候当前线程睡眠1000毫秒
        Thread.sleep(1000);*/
        /**
         * 线程分为    守护线程    用户线程
         * 守护线程     设置为守护线程  t1.setDaemon(true)
         *  getPriority() ：返回线程优先值
         *  setPriority(int newPriority) ：改变线程的优先级
         *
         *  线程创建时继承父线程的优先级
         *
         * 用户
         */
    }
    static class MyRunnable4 implements Runnable{
        @Override
        public void run() {
            int count = 0;
            int size = 0;
            File file = new File("E:\\");
            File[] files = file.listFiles();
            for(int i = 0; i < files.length;i++){
                if(!files[i].isDirectory()){
                    count++;
                    size += files[i].length();
                    if(Thread.currentThread().getName().equals("第二个")){
                        System.out.println("文件名称"+files[i].getName());
                        System.out.println("当前文件大小"+files[i].length());
                    }
                }
            }
            if(Thread.currentThread().getName().equals("第一个")){
                System.out.println("总个数"+count);
                System.out.println("总大小"+size);
            }
        }
    }
    static class MyRunnable3 implements Runnable{
        static boolean flag = false;
        @Override
        public void run() {
            int i = 1;
            for (;;){
                if(flag){
                    break;
                }
                System.out.println(i++);
            }
        }
    }
    static class MyRunnable2 implements Runnable{
        static List<Integer> list = new ArrayList<>();
        @Override
        public synchronized void run() {
            int i1 = (int) (Math.random() * 20);
            for(int i =0 ;i<=100;i++){
                try {
                    Thread.sleep(i1);
                } catch (InterruptedException e) {
                }
                int random = (int) (Math.random()*100);
                System.out.println(random);
                list.add(random);
            }
            try {
                Thread.sleep(1000*30);
            } catch (InterruptedException e){
                Thread.interrupted();
            }
            System.out.println(list);
        }
    }
    static class MyRunnable1 implements Runnable{
        int a = 100;
        @SneakyThrows
        @Override
        public synchronized void run() {
            for(int i = 1; i <= a;i++){
                if(i%2 == 0 && Thread.currentThread().getName().equals("双数")){
                    System.out.println(Thread.currentThread()+":"+i);
                }else if(i%2 != 0 && Thread.currentThread().getName().equals("单数")){
                    System.out.println(Thread.currentThread()+":"+i);
                }
            }
        }
    }
    static class MyThread extends Thread{
        static int a = 100;
        @SneakyThrows
        @Override
        public synchronized void run() {
            for (int i = 0; i < a;){
                Thread.sleep(100);
                a--;
                System.out.println(Thread.currentThread()+"在买票:余票"+a);
            }
        }
    }
    static class MyRunnable implements Runnable{
        int a = 100;
        @SneakyThrows
        @Override
        public synchronized void run() {
            Thread.sleep(500);
            for (int i = 0; i < a;){
                a--;
                System.out.println(Thread.currentThread()+"在买票:余票"+a);
            }
        }
    }
}
